package com.hongyuan.sellfitness.ui.home.activity;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import androidx.lifecycle.ViewModelProviders;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.databinding.ActivityStoreGroupDetailsBinding;
import com.hongyuan.sellfitness.ui.home.view_mode.StoreGroupDetailViewModel;
import com.hongyuan.sellfitness.utils.CustomDialog;
import com.hongyuan.sellfitness.utils.HourMeterUtil;
import com.hongyuan.sellfitness.utils.TimeUtil;
import com.hongyuan.sellfitness.utils.ViewChangeUtil;

public class StoreGroupDetailActivity extends CustomActivity<ActivityStoreGroupDetailsBinding, StoreGroupDetailViewModel> {

    //计时器
    private HourMeterUtil hourMeterUtil;
    //是否已设置提醒
    private boolean isRend = false;
    //报名时间与当前时间差值
    private long subTime;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_store_group_details;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public StoreGroupDetailViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(StoreGroupDetailViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X4);

        binding.comBack.setOnClickListener(view -> finish());
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.clearParams().setParams("cs_id",getBundle().getString("cs_id"));

        viewModel.requestData(Constants.GET_SOURSE_SUPER_INFO);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.changeRemind.observe(this, aVoid -> {
            if(isRend){
                binding.setRemind.setText("取消提醒");
            }else{
                binding.setRemind.setText("设置提醒");
            }
        });

        viewModel.uc.qdSuccess.observe(this, aVoid -> {
            binding.goEnterName.setClickable(false);
            binding.goEnterName.setBackgroundResource(R.drawable.shape_radius6_999999);
            binding.goEnterName.setText("已签到");
            CustomDialog.groupCoursePunchSuccess(this, TimeUtil.formatDataMsec(TimeUtil.dateFormatDotMD,System.currentTimeMillis()),
                    TimeUtil.getWeek());
        });

        viewModel.uc.changeShow.observe(this, storeGroupDetailBean -> {
            //四种报名状态
            final int NO_RESERVATION = 0;//不可预约
            final int CAN_RESERVATION = 1;//可预约
            final int TANSION = 2;//紧张
            final int FULL = 3;//满
            final int REGISTERED = 4;//已预约
            final int NOT_OPPN = 5;//未开放
            int toState = Integer.valueOf(storeGroupDetailBean.getTo_state());

            if(toState == CAN_RESERVATION || toState == TANSION){
                if(toState == CAN_RESERVATION){
                    //报名状态显示
                    binding.goEnterName.setText("报名中");
                }
                if(toState == TANSION){
                    //报名状态显示
                    binding.goEnterName.setText("报名中(紧张)");
                }
                binding.goEnterName.setClickable(true);
                binding.goEnterName.setBackgroundResource(R.drawable.shape_gradient_v_radiu6_login);
                binding.goEnterName.setOnClickListener(v -> {
                    Bundle bundle = new Bundle();
                    bundle.putString("cs_id",getBundle().getString("cs_id"));
                    startActivity(SignUpGroupActivity.class,bundle);
                });
            }else{
                binding.goEnterName.setBackgroundResource(R.drawable.shape_radius6_999999);
                if(toState == NO_RESERVATION){
                    binding.goEnterName.setClickable(false);
                    binding.goEnterName.setText("不可预约");
                }else if(toState == FULL){
                    binding.goEnterName.setClickable(false);
                    binding.goEnterName.setText("已满");
                }else if(toState == REGISTERED){

                    binding.goEnterName.setText("已预约");

                    //距离课程开始相差的分钟数
                    int differenceStart = TimeUtil.getOffectMinutes(storeGroupDetailBean.getCs_start_time()*1000,storeGroupDetailBean.getNow_time()*1000);
                    //距离课程结束相差的分钟数
                    int differenceEnd = TimeUtil.getOffectMinutes(storeGroupDetailBean.getCs_end_time()*1000,storeGroupDetailBean.getNow_time()*1000);
                    if(differenceStart > 30){
                        binding.goEnterName.setClickable(true);

                        binding.goEnterName.setText("取消报名");
                        binding.goEnterName.setOnClickListener(v -> {
                            CustomDialog.promptDialog(this, "确定取消报名该团课吗？", "确定取消", "暂不取消", false, v1 -> {
                                if(v1.getId() == R.id.isOk){
                                    //调用取消预约接口
                                    viewModel.setParams("ocs_id",String.valueOf(storeGroupDetailBean.getOcs_id()));
                                    viewModel.requestData(Constants.CANCEL_COURSE_SUPER_ORDER);
                                }
                            });
                        });
                    }else if(differenceStart <= 30 && differenceEnd >= 0){

                        if("0".equals(storeGroupDetailBean.getIs_qd())){//未签到时
                            //点击去签到
                            binding.goEnterName.setText("扫码签到");
                            binding.goEnterName.setClickable(true);
                            binding.goEnterName.setBackgroundResource(R.drawable.shape_gradient_v_radiu6_login);
                            binding.goEnterName.setOnClickListener(v -> {
                                goScan();
                            });
                        }else if("1".equals(storeGroupDetailBean.getIs_qd())){//已签到
                            //设置不可点击
                            binding.goEnterName.setText("已签到");
                            binding.goEnterName.setClickable(false);
                        }else{
                            //设置不可点击
                            binding.goEnterName.setText("未知状态");
                            binding.goEnterName.setClickable(false);
                        }

                    }else{
                        if("0".equals(storeGroupDetailBean.getIs_qd())) {//未签到时
                            binding.goEnterName.setText("未签到");
                            binding.goEnterName.setClickable(false);
                        }else if("1".equals(storeGroupDetailBean.getIs_qd())){//已签到
                            //设置不可点击
                            binding.goEnterName.setText("已签到");
                            binding.goEnterName.setClickable(false);
                        }else{
                            //设置不可点击
                            binding.goEnterName.setText("未知状态");
                            binding.goEnterName.setClickable(false);
                        }
                    }

                }else if(toState == NOT_OPPN){
                    binding.goEnterName.setText("未开放");
                    binding.goEnterName.setClickable(false);
                }else{
                    binding.goEnterName.setText("不可预约");
                    binding.goEnterName.setClickable(false);
                }
            }


            //报名时间与当前时间差值
            subTime = storeGroupDetailBean.getBm_time() - storeGroupDetailBean.getNow_time();
            //是否显示报名倒计时提醒（）
            if(storeGroupDetailBean.getBm_time() != 0 && subTime > 0){
                binding.goEnterName.setText("未到报名时间");
                binding.goEnterName.setClickable(false);

                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(binding.remindBox,"translationX",0f,120f,0f);
                objectAnimator.setDuration(500);
                objectAnimator.start();

                binding.remindBox.setVisibility(View.VISIBLE);
                hourMeterUtil = new HourMeterUtil();
                //毫秒差
                //long haoMiao = TimeUtil.getDifferenceNow(detailBean.getData().getAdd_date(),TimeUtil.dateFormatYMDHMS);
                hourMeterUtil.setTimeCallBack(passedTime -> {
                    subTime--;

                    if(subTime > 0){
                        binding.showCountdown.setText(TimeUtil.getDaysTime(subTime));
                    }else{
                        binding.remindBox.setVisibility(View.GONE);
                        //去刷新下页面，让其可以报名
                        //viewModel.requestData(Constants.GET_SOURSE_SUPER_INFO);
                    }

                });
                hourMeterUtil.startCount();

                binding.setRemind.setOnClickListener(v -> {
                    if(isRend){
                        ViewChangeUtil.changeLeftDrawable(this,binding.setRemind,R.mipmap.baise_yuanxing_default_mark);
                        isRend = false;
                        //关闭团课预约提醒
                        viewModel.requestData(Constants.DEL_REMIND_CS);
                    }else{
                        ViewChangeUtil.changeLeftDrawable(this,binding.setRemind,R.mipmap.baise_yuanxing_select_mark);
                        isRend = true;
                        //开启团课预约提醒
                        viewModel.requestData(Constants.ADD_REMIND_CS);
                    }
                });
            }else{
                binding.remindBox.setVisibility(View.GONE);
            }

            //是否是会员
            if("1".equals(storeGroupDetailBean.getIs_hy())){
                binding.signUpBox.setVisibility(View.VISIBLE);
                binding.buyCardBox.setVisibility(View.GONE);
            }else{
                binding.signUpBox.setVisibility(View.GONE);
                binding.buyCardBox.setVisibility(View.VISIBLE);
            }

            //设置提醒
            if("1".equals(storeGroupDetailBean.getIs_remind())){
                isRend = true;
                ViewChangeUtil.changeLeftDrawable(this,binding.setRemind,R.mipmap.baise_yuanxing_select_mark);
                binding.setRemind.setText("取消提醒");
            }else{
                isRend = false;
                ViewChangeUtil.changeLeftDrawable(this,binding.setRemind,R.mipmap.baise_yuanxing_default_mark);
                binding.setRemind.setText("设置提醒");
            }
        });
    }
}
